home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / arrays / arrays.txt < prev    next >
Encoding:
Text File  |  1997-08-11  |  10.9 KB  |  321 lines

  1.  
  2.                   Array Structures in Visual Basic
  3. -----------------------------------------------------------------------
  4.  
  5.    After viewing much source code from differnet sources I've found
  6. very common programming styles that lend themselves to hogging system
  7. resources, and slowing down programs in general. Many beginning
  8. programmers also end up making programming much more difficult on
  9. themselves in the process. We're going to learn a new word today.
  10.  
  11.                              ARRAYS
  12.                              ------
  13.  
  14.    Learn it. Live it. Love it. First of all, Arrays make programs run
  15. much faster. Everyone likes fast executing programs. Fast is good.
  16. Slow is bad. If you like your program to run really slow for no reason,
  17. then don't waste your time reading any further. However, if you want
  18. to learn how to make your code execute fast and save yourself lots of
  19. typing too, read on.
  20.  
  21.    Just about any object in Visual Basic can be assigned an Array
  22. Index. The Index is an identifier that allows you to access the
  23. variable based on a set of conditions. The Array value is set at
  24. design time in the (you guessed it) INDEX property. Any object with an
  25. Index property can be placed into an Array.
  26.  
  27.    So *that's* what Index is for...
  28.  
  29.    Now, using Arrays is not designed for every object in a program,
  30. but is extremely helpful when the object in question is duplicated
  31. several times throughout a program. If your object is the only object
  32. of it's type that is accessed the same way, It's not worth assigning
  33. Array values to that object. It won't speed anything up or make
  34. anything easier if this is the case.
  35.    
  36.    Let's look at how much easier it is to control an object with an
  37. Array Index than with variables. We need an example that's at least
  38. somewhat interesting, so we'll use an Image control that will make our
  39. program look a little different than others. We're going to make our
  40. own Pop-Up menu, that looks cooler than the "built in" one. Rather
  41. than have highlighted options, we're going to use "outlined" options
  42. on our Pop-Up menu.
  43.  
  44.    To get the "outline" look, we'll simply change the BorderStyle
  45. property of a Label control from "0; None", to "1; Fixed Single" when
  46. the Mouse Cursor is over the "highlighted" option. We're going to have
  47. 10 (ten) different options, simply to illustrate the effects of an
  48. Array over coding each label separartely. The higher the number of
  49. objects (and their properties) being controlled by the Array, the
  50. easier it is on the programmer, and the faster the program will run.
  51.  
  52.    For this example, our Pop-Up menu will work when we Click a Command
  53. Button on Form1.
  54.  
  55.    So, what do we need? Let's see... A start up form (Form1) with a
  56. Command Button control (Command1) and our Pop-Up Menu with 10 Label
  57. controls that will appear when we Click the Command Button on Form1,
  58. and disappear when we select "Cancel". We'll make another form (Form2)
  59. and use it for our actual Pop-Up Menu. We need to add 10 Labels onto
  60. Form2 and make each Option "do something" when we click on it. For
  61. now, we'll make the Command Button Control on our main form display
  62. which Option on our Pop-Up Menu was selected, unless it was "Cancel",
  63. in which case we'll make the Pop-Up Menu disappear.
  64.  
  65.    Here's the code without using an Array:
  66.  
  67. ----------
  68.  
  69. Private Sub Label1_Click()
  70.    Form1.Command1.Caption = "Label1"
  71. End Sub
  72.  
  73. Private Sub Label1_MouseMove(Button As Integer, Shift As Integer,
  74.                              X As Single, Y As Single)
  75.    Label1.BorderStyle = 1
  76.    Label2.BorderStyle = 0
  77.    Label3.BorderStyle = 0
  78.    Label4.BorderStyle = 0
  79.    Label5.BorderStyle = 0
  80.    Label6.BorderStyle = 0
  81.    Label7.BorderStyle = 0
  82.    Label8.BorderStyle = 0
  83.    Label9.BorderStyle = 0
  84.    Label10.BorderStyle = 0
  85. End Sub
  86.  
  87. Private Sub Label2_Click()
  88.    Form1.Command1.Caption = "Label2"
  89. End Sub
  90.  
  91. Private Sub Label2_MouseMove(Button As Integer, Shift As Integer,
  92.                              X As Single, Y As Single)
  93.    Label1.BorderStyle = 0
  94.    Label2.BorderStyle = 1
  95.    Label3.BorderStyle = 0
  96.    Label4.BorderStyle = 0
  97.    Label5.BorderStyle = 0
  98.    Label6.BorderStyle = 0
  99.    Label7.BorderStyle = 0
  100.    Label8.BorderStyle = 0
  101.    Label9.BorderStyle = 0
  102.    Label10.BorderStyle = 0
  103. End Sub
  104.  
  105. Private Sub Label3_Click()
  106.    Form1.Command1.Caption = "Label3"
  107. End Sub
  108.  
  109. Private Sub Label3_MouseMove(Button As Integer, Shift As Integer,
  110.                              X As Single, Y As Single)
  111.    Label1.BorderStyle = 0
  112.    Label2.BorderStyle = 0
  113.    Label3.BorderStyle = 1
  114.    Label4.BorderStyle = 0
  115.    Label5.BorderStyle = 0
  116.    Label6.BorderStyle = 0
  117.    Label7.BorderStyle = 0
  118.    Label8.BorderStyle = 0
  119.    Label9.BorderStyle = 0
  120.    Label10.BorderStyle = 0
  121. End Sub
  122.  
  123. Private Sub Label4_Click()
  124.    Form1.Command1.Caption = "Label4"
  125. End Sub
  126.  
  127. Private Sub Label4_MouseMove(Button As Integer, Shift As Integer,
  128.                              X As Single, Y As Single)
  129.    Label1.BorderStyle = 0
  130.    Label2.BorderStyle = 0
  131.    Label3.BorderStyle = 0
  132.    Label4.BorderStyle = 1
  133.    Label5.BorderStyle = 0
  134.    Label6.BorderStyle = 0
  135.    Label7.BorderStyle = 0
  136.    Label8.BorderStyle = 0
  137.    Label9.BorderStyle = 0
  138.    Label10.BorderStyle = 0
  139. End Sub
  140.  
  141. Private Sub Label5_Click()
  142.    Form1.Command1.Caption = "Label5"
  143. End Sub
  144.  
  145. Private Sub Label5_MouseMove(Button As Integer, Shift As Integer,
  146.                              X As Single, Y As Single)
  147.    Label1.BorderStyle = 0
  148.    Label2.BorderStyle = 0
  149.    Label3.BorderStyle = 0
  150.    Label4.BorderStyle = 0
  151.    Label5.BorderStyle = 1
  152.    Label6.BorderStyle = 0
  153.    Label7.BorderStyle = 0
  154.    Label8.BorderStyle = 0
  155.    Label9.BorderStyle = 0
  156.    Label10.BorderStyle = 0
  157. End Sub
  158.  
  159. Private Sub Label6_Click()
  160.    Form1.Command1.Caption = "Label6"
  161. End Sub
  162.  
  163. Private Sub Label6_MouseMove(Button As Integer, Shift As Integer,
  164.                              X As Single, Y As Single)
  165.    Label1.BorderStyle = 0
  166.    Label2.BorderStyle = 0
  167.    Label3.BorderStyle = 0
  168.    Label4.BorderStyle = 0
  169.    Label5.BorderStyle = 0
  170.    Label6.BorderStyle = 1
  171.    Label7.BorderStyle = 0
  172.    Label8.BorderStyle = 0
  173.    Label9.BorderStyle = 0
  174.    Label10.BorderStyle = 0
  175. End Sub
  176.  
  177. Private Sub Label7_Click()
  178.    Form1.Command1.Caption = "Label7"
  179. End Sub
  180.  
  181. Private Sub Label7_MouseMove(Button As Integer, Shift As Integer,
  182.                              X As Single, Y As Single)
  183.    Label1.BorderStyle = 0
  184.    Label2.BorderStyle = 0
  185.    Label3.BorderStyle = 0
  186.    Label4.BorderStyle = 0
  187.    Label5.BorderStyle = 0
  188.    Label6.BorderStyle = 0
  189.    Label7.BorderStyle = 1
  190.    Label8.BorderStyle = 0
  191.    Label9.BorderStyle = 0
  192.    Label10.BorderStyle = 0
  193. End Sub
  194.  
  195. Private Sub Label8_Click()
  196.    Form1.Command1.Caption = "Label8"
  197. End Sub
  198.  
  199. Private Sub Label8_MouseMove(Button As Integer, Shift As Integer,
  200.                              X As Single, Y As Single)
  201.    Label1.BorderStyle = 0
  202.    Label2.BorderStyle = 0
  203.    Label3.BorderStyle = 0
  204.    Label4.BorderStyle = 0
  205.    Label5.BorderStyle = 0
  206.    Label6.BorderStyle = 0
  207.    Label7.BorderStyle = 0
  208.    Label8.BorderStyle = 1
  209.    Label9.BorderStyle = 0
  210.    Label10.BorderStyle = 0
  211. End Sub
  212.  
  213. Private Sub Label9_Click()
  214.    Form1.Command1.Caption = "Label9"
  215. End Sub
  216.  
  217. Private Sub Label9_MouseMove(Button As Integer, Shift As Integer,
  218.                              X As Single, Y As Single)
  219.    Label1.BorderStyle = 0
  220.    Label2.BorderStyle = 0
  221.    Label3.BorderStyle = 0
  222.    Label4.BorderStyle = 0
  223.    Label5.BorderStyle = 0
  224.    Label6.BorderStyle = 0
  225.    Label7.BorderStyle = 0
  226.    Label8.BorderStyle = 0
  227.    Label9.BorderStyle = 1
  228.    Label10.BorderStyle = 0
  229. End Sub
  230.  
  231. Private Sub Label10_Click()
  232.    Label10.BorderStyle = 0
  233.    Form1.Command1.Caption = "Click Me"
  234.    Form2.Visible = False
  235. End Sub
  236.  
  237. Private Sub Label10_MouseMove(Button As Integer, Shift As Integer,
  238.                               X As Single, Y As Single)
  239.    Label1.BorderStyle = 0
  240.    Label2.BorderStyle = 0
  241.    Label3.BorderStyle = 0
  242.    Label4.BorderStyle = 0
  243.    Label5.BorderStyle = 0
  244.    Label6.BorderStyle = 0
  245.    Label7.BorderStyle = 0
  246.    Label8.BorderStyle = 0
  247.    Label9.BorderStyle = 0
  248.    Label10.BorderStyle = 1
  249. End Sub
  250.  
  251. ----------
  252.  
  253.    Kinda long huh? Now, let's use an Array and see how much easier it
  254. could have been. Both sets of source code accomplish the same thing.
  255.  
  256. ----------
  257.  
  258. Private Sub Label1_Click(Index As Integer)
  259.    Form1.Command1.Caption = Label1(Index).Caption
  260.    If Index = 10 Then
  261.       Label1(10).BorderStyle = 0
  262.       Form1.Command1.Caption = "Click Me"
  263.       Form2.Visible = False
  264.    End If
  265. End Sub
  266.  
  267. Private Sub Label1_MouseMove(Index As Integer, Button As Integer,
  268.                          Shift As Integer, X As Single, Y As Single)
  269.    Dim n As Integer
  270.    For n = 1 To 10
  271.       Label1(n).BorderStyle = 0
  272.    Next
  273.    Label1(Index).BorderStyle = 1
  274. End Sub
  275.  
  276. ----------
  277.  
  278.    That's it? Gee whiz Wally! I would have worn out my keyboard coding
  279. that without Arrays!
  280.  
  281.    Each Label (control) in our Array needs to be named the same thing
  282. (Label1) and have an Index number in the Index property field to
  283. reference. All we need to do to use the Array, is assign an Index
  284. value on each Label control on Form2. The Index number we assign will
  285. correspond with each Label in the Array. 
  286.  
  287.    Note: If you are *changing* an *existing* set of controls, you must
  288. first remove the separate controls' code, or you may have problems
  289. getting the Visual Basic IDE to correctly name your "new" Array of
  290. controls. This happens when the control is named the same as a pre-
  291. existing control. Simply remove the old code, which will now be in the
  292. Form's "Declarations", before placing any code into the new Array.
  293.  
  294.    As a final note, this code only shows how and why to use an Array.
  295. If you actually want your own Label Control Menu System, you shouldn't
  296. turn "OFF" (set BorderStyle = 0) all of the Labels' BorderStyle
  297. properties each time the MouseMove Event is triggered, then turn back
  298. "ON" the current one (like we have done in this example, just to make
  299. it easy). 
  300.  
  301.    Even though the coding is much simpler, you'll still be executing
  302. many more instructions than are necessary in your program. Try finding
  303. which Label is the "current" one and only "turn off" only that one
  304. when a new Label is "highlighted", so that your program isn't
  305. resetting 10 (or more) property values for each pixel the mouse moves.
  306. This defeats half the reason to use the array in the first place (in
  307. this example).
  308.  
  309.    The Projects HARDWAY.VBP and EASYWAY.VBP are the example files for
  310. this tutorial. They both use the same Start Up form (ARRAY.FRM), but
  311. different "Menu" forms (HARDWAY.FRM and EASYWAY.FRM). The example
  312. files were written in VB4.0 Professional, but should work with any of
  313. the Visual Basic for Windows versions (that I'm aware of). The only
  314. controls used are a single Command Button and 10 Labels. All example
  315. files are archived into ARRAYS.ZIP. 
  316.  
  317. Copyright (c) 1997 Russ Ricca
  318. russ@spinward.com
  319.  
  320. Content of this text may be freely reproduced in it's complete form.
  321.